package cn.lzd.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import cn.lzd.ui.R;

/**
 * @author lizhidong
 * recycle View 的滑动指示器
 * 在调用 {@link SmoothBarView#registerRecycleView(RecyclerView)} 之后，对 RecycleView 开始生效
 * 相关参数可以设置一些颜色，及比例、方向
 */
public class SmoothBarView extends View {
    private final boolean isVertical;
    private final float foregroundPercent;

    private float nowRatio = 0f;
    private final Paint backgroundPaint;
    private final Paint foregroundPaint;
    private final RectF backgroundRect;
    private final RectF foregroundRect;

    public SmoothBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(R.styleable.SmoothBarView);
        isVertical = typedArray.getBoolean(R.styleable.SmoothBarView_sbv_orientationVertical, false);
        @ColorInt int backgroundColor = typedArray.getColor(R.styleable.SmoothBarView_sbv_bgColor,
                ContextCompat.getColor(context, R.color.base_ui_white));
        @ColorInt int foregroundColor = typedArray.getColor(R.styleable.SmoothBarView_sbv_foregroundColor,
                ContextCompat.getColor(context, R.color.base_ui_warn));
        foregroundPercent = typedArray.getFloat(R.styleable.SmoothBarView_sbv_foregroundPercent, 0.5f);
        typedArray.recycle();


        // region init Paint
        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(backgroundColor);
        backgroundRect = new RectF();

        foregroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        foregroundPaint.setColor(foregroundColor);
        foregroundRect = new RectF();
        // endregion
    }

    public void registerRecycleView(RecyclerView target) {
        if (target == null) {
            return;
        }
        target.addOnScrollListener(targetListener);
    }

    private final RecyclerView.OnScrollListener targetListener = new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int range, offset;
            if (isVertical) {
                range = recyclerView.computeVerticalScrollRange() - recyclerView.computeVerticalScrollExtent();
                offset = recyclerView.computeVerticalScrollOffset();
            } else {
                range = recyclerView.computeHorizontalScrollRange() - recyclerView.computeHorizontalScrollExtent();
                offset = recyclerView.computeHorizontalScrollOffset();
            }
            nowRatio = (float) offset / range;
            invalidate();
        }
    };

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        backgroundRect.set(0, 0, width, height);
        int roundSize = Math.min(width, height) / 2;
        canvas.drawRoundRect(backgroundRect, roundSize, roundSize, backgroundPaint);

        float foregroundLen = (isVertical ? height : width) * foregroundPercent;
        float foreOffset = nowRatio * ((isVertical ? height : width) - foregroundLen);
        foregroundRect.set(
                isVertical ? 0 : foreOffset,
                isVertical ? foreOffset : 0,
                isVertical ? width : foregroundLen + foreOffset,
                isVertical ? foregroundLen + foreOffset : height
        );
        canvas.drawRoundRect(foregroundRect, roundSize, roundSize, foregroundPaint);
    }
}
